package com.example.javaproject.datastructure.test;

import java.util.Stack;

/**
 * Description: 利用2个栈实现队列
 * <br>Author：tian
 * @see <a href="https://blog.csdn.net/u011328417/article/details/79436120">实现原理</a>
 * <br>Time: 2019/3/12 14:47
 */
public class TwoStackToQue {
    private Stack<String> s1 = new Stack<>();
    private Stack<String> s2 = new Stack<>();

    private int mSize;


    public void push(String value) {
        s1.push(value);
        mSize++;
    }

    public String pop() {
        String popItem = null;

        if (s1.isEmpty() && s2.isEmpty()) {
            return popItem;
        }

        if (s2.isEmpty()) {
            while (s1.size() > 1) {
                s2.push(s1.pop());
            }

            popItem = s1.pop();
        }
        //s2有值
        else {
            popItem = s2.pop();
        }

        return popItem;
    }

    public static void main(String[] args) {
        TwoStackToQue twoStackToQue = new TwoStackToQue();

        twoStackToQue.push("a");
        twoStackToQue.push("b");
        twoStackToQue.push("c");
        twoStackToQue.push("d");
        twoStackToQue.push("e");

        for (int i = 0; i < twoStackToQue.mSize; i++) {
            System.out.println(twoStackToQue.pop());
        }
    }


}
